const axios = require('axios');
const fs = require('fs');
const path = require("path");
import { Db, MongoClient } from "mongodb";

const imgPath = path.resolve(__dirname, "images");

// start();
link();

async function link(){
    const url = "mongodb://admin:jsz04005301@120.48.63.39:27017";
    const client = new MongoClient(url);
    await client.connect();
    console.log("Connected successfully to server!");
    const db = client.db("book");
    const ary = await db.find({}).toArray();
    console.log(ary);
}


async function start() {
    makeDirs(imgPath);

    let url = 'https://www8.garmin.com/manuals-apac/webhelp/forerunner245245music/ZH-TW/static/images/temp/logo.png';
    download(url);
}

async function download(url) {
    const res = await axios.get(url, { responseType: 'arraybuffer' });
    const fname = path.resolve(imgPath, Date.now() + ".jpg")
    fs.writeFileSync(fname, res.data);
}

function makeDirs(dir) {
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
}